// maindlg.cpp : implementation of the CMediaListDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "medialistdlg.h"
#include "UIHelper.h"

#include "constdef.h"


LRESULT CMediaListDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	HWND hwndMB = AtlCreateMenuBar(m_hWnd, IDR_MEIDAMENU,SHCMBF_HMENU);

	// center the dialog on the screen
	CenterWindow(GetParent());

	DlgResize_Init(false);
	SHInitMaxDialog(m_hWnd);


	m_idcList.SubclassWindow(GetDlgItem(IDC_MEDIALIST));
	m_idcList.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);


	m_imgList.Create(16, 16, ILC_COLOR, 4, 8);
	HBITMAP hBMP = LoadBitmap(_Module.GetResourceInstance() ,MAKEINTRESOURCE(IDB_BITMAP_MEDIA));
	m_imgList.Add(hBMP);
	m_imgList.Add(NULL);
	m_idcList.SetImageList(m_imgList, LVSIL_SMALL);


	//Init list control
	RECT rcList;
	m_idcList.GetClientRect(&rcList); 

	CString strCol;
	strCol.LoadString(IDS_MEDIA);
	m_idcList.InsertColumn(0, strCol, LVCFMT_LEFT, rcList.right - rcList.left);


	LoadMediaList();
	return TRUE;
}

LRESULT CMediaListDlg::OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	EndDialog(wID);
	return 0;
}

LRESULT CMediaListDlg::OnItemChanged(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	//UpdateMenu();

	return 0L;
}

LRESULT CMediaListDlg::OnListClick(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	//DoPlayMedia(NULL);
	DownLoadMediaAndPlay();
	return 0L;
}

LRESULT CMediaListDlg::OnPlayCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	//DoPlayMedia(NULL);
	DownLoadMediaAndPlay();
	return 0L;
}

int CMediaListDlg::LoadMediaList()
{
	//get data from file
	m_mediaArray.RemoveAll();


	//get list from srv
	//...

	FILE *fp;
	fp = fopen(A_TEMPMEDIALIST, "r+b");
	if(fp) 
	{
		char buf[MAX_PATH] = {0};
		TCHAR wBuf[MAX_PATH] = {0};
		while (fgets(buf,MAX_PATH,fp))
		{
			//swprintf(wBuf,L"%S",buf);
			MultiByteToWideChar(CP_ACP, 0, buf, -1, wBuf, MAX_PATH);
			if(_tcslen(wBuf)<_tcslen(U_MEDIAEXT)+1) continue;

			int len = _tcslen(wBuf);
			if( (wBuf[len-2] == '\r') && (wBuf[len-1] == '\n')) //'\r\n'
			{
				len -=2;
			}

			TCHAR tmp[MAX_PATH] = {0};
			_tcsncpy(tmp,wBuf,len);

			m_mediaArray.Add(*(TPath*)tmp);
		}
		fclose(fp);
	}
	

	//update listview
	m_idcList.DeleteAllItems();
	int nIndex=0;
	for(int i=0;i<m_mediaArray.GetSize();i++)
	{
		m_idcList.InsertItem(nIndex, m_mediaArray[i].path, 0);
		nIndex++;
	}

	return nIndex;
}


void CMediaListDlg::DoPlayMedia(TCHAR* name)
{
	//check player.exe is exist
	if(!IsFileExist(U_MEDIAPLAYERNAME)) return;

	//get filename
// 	TCHAR filename[256] = {0};
// 	int nIndex = m_idcList.GetNextItem(-1, LVNI_SELECTED);
// 
// 	if(nIndex < 0)
// 	{
// 		CString caption;
// 		CString msg;
// 		caption.LoadString(IDS_ERR_CAP);
// 		msg.LoadString(IDS_ERR_SELECTFILE);
// 		MessageBox(msg,  caption);
// 		return;
// 	}

// 	BSTR strText=NULL;
// 	m_idcList.GetItemText(nIndex, 0, strText);
// 
// 	_tcscpy(filename,U_MYMEDIADATADIR);
// 	if(filename[_tcslen(filename)-1] != _T('\\'))
// 		_tcscat(filename,_T("\\"));
// 
// 	_tcscat(filename,strText);

	if(!IsFileExist(name))	return;
	
	//play media
	STLOG_WRITE(name);
	PROCESS_INFORMATION *pProcInfo = NULL;
	::CreateProcess(U_MEDIAPLAYERNAME, name, NULL, NULL, FALSE, 0, NULL, NULL, NULL, pProcInfo);
}

void CMediaListDlg::DownLoadMediaAndPlay()
{
	if(m_Conn==NULL || m_Config==NULL) return;
	
	//get filename
	int nIndex = m_idcList.GetNextItem(-1, LVNI_SELECTED);
	if(nIndex < 0)
	{
		CString caption;
		CString msg;
		caption.LoadString(IDS_ERR_CAP);
		msg.LoadString(IDS_ERR_SELECTFILE);
		MessageBox(msg,  caption);
		return;
	}

	::SetCursor(::LoadCursor(NULL, IDC_WAIT));
	BSTR strText=NULL;
	m_idcList.GetItemText(nIndex, 0, strText);

	char filename[MAX_PATH] = {0};
	//wcstombs(filename, strText, wcslen(strText)+1 );
	//sprintf(filename,"%S",strText);
	DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,strText,-1,NULL,0,NULL,FALSE);
	WideCharToMultiByte (CP_OEMCP,NULL,strText,-1,filename,dwNum,NULL,FALSE);

	
	char tagfile[MAX_PATH] = {0};
	strcpy(tagfile,A_TEMPFILEDIR);
	if(tagfile[strlen(tagfile)-1] != '\\')
		strcat(tagfile,"\\");

	strcat(tagfile,filename);

	TCHAR wTagFile[MAX_PATH] = {0};
	//MultiByteToWideChar(CP_ACP, 0, tagfile, -1, wTagFile, MAX_PATH);
	swprintf(wTagFile,L"%S",tagfile);

// 	TCHAR ext[32] = {0};
// 	SepExtAndName(wTagFile,ext);
// 
// 	memset(wTagFile,0,MAX_PATH);
// 	_tcscpy(wTagFile,U_MYMEDIATMPFILE);
// 	_tcscat(wTagFile,ext);


	int filedowned = 0;
	
	DeleteFile(U_TEMPMEDIAFILE);
	filedowned = m_Conn->GetMediaFile(m_Config->m_UserInfo.curFriend,filename,A_TEMPMEDIAFILE);

	STLOG_WRITE("GetMediaFile");

	::SetCursor(NULL);

	if(filedowned == 200)
	{
		STLOG_WRITE("DoPlayMedia");
		DoPlayMedia(U_TEMPMEDIAFILE);
	}
	else
	{
		CString caption;
		CString msg;
		caption.LoadString(IDS_ERR_CAP);
		msg.LoadString(IDS_ERR_DOWNLOAD);
		MessageBox(msg,  caption);
	}
}